package com.nice.nice201110.service;

import com.nice.nice201110.entity.TBaseDisease;
import com.nice.nice201110.entity.TBaseInformation;
import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.mapper.TBaseInformationMapper;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TbaseInformationService {
    @Autowired
    TBaseInformationMapper tBaseInformationMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /**  查询数据 */
    public List<TBaseInformation> getlist(BaseVo baseVo){
        return tBaseInformationMapper.selectByPrimaryKeys(baseVo);
    }
    /** 查询单一数据 */
    public TBaseInformation getOne(UserVo userVo,Integer id){
        TBaseInformation tBaseInformation = tBaseInformationMapper.selectByPrimaryKey(id);
        if(tBaseInformation!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tBaseInformation.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_base_information);
            tLog.setRemark("查看基本信息:"+tBaseInformation.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tBaseInformation;
    }
    /** 插入数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean insertTbaseInformation(UserVo userVo,TBaseInformation tBaseInformation){
        try {
            int num = tBaseInformationMapper.insertSelective(tBaseInformation);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseInformation.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_information);
                tLog.setRemark("新增基本信息:"+tBaseInformation.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 更新数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean updateTbaseInformation(UserVo userVo,TBaseInformation tBaseInformation){
        try {
            int num = tBaseInformationMapper.updateByPrimaryKeySelective(tBaseInformation);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseInformation.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_information);
                tLog.setRemark("修改基本信息:"+tBaseInformation.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除单一数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetBaseInformation(UserVo userVo,Integer id){
        try {
            int num = tBaseInformationMapper.deleteSelective(id);
            if (num > 0) {
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id); //  数据id
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_information);
                tLog.setRemark("删除基本信息:" + tBaseInformationMapper.selectByPrimaryKey(id).toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除多数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetsBaseInformation(UserVo userVo, String ids){
        String StrArr [] = ids.split(",");
        try {
            for(String strID:StrArr) {
                if(strID!=null && strID.trim().length()>0) {
                    int id = Integer.valueOf(strID);
                    int num = tBaseInformationMapper.deleteSelective(id);
                    if (num > 0) {
                        tLog.setDate(new Date());
                        tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                        tLog.setDocid(id); //  数据id
                        tLog.setUsername(userVo.getName());
                        tLog.setUserid(Integer.valueOf(userVo.getId()));
                        tLog.setTtable(NiceTableEnum.form_base_information);
                        tLog.setRemark("删除基本信息:" + tBaseInformationMapper.selectByPrimaryKey(id).toString());
                        tLogMapper.insertSelective(tLog);
                    }else{
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
